package com.angel.service.ServiceImpl;

import com.angel.bean.IllCase;
import com.angel.service.IllCaseService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.rmi.server.ExportException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angel.bean.Prescription;
import com.angel.mapper.PrescriptionDao;
import com.angel.service.PrescriptionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class PrescriptionServiceImpl implements PrescriptionService{

    @Resource
    private PrescriptionDao prescriptionDao;

    @Resource
    private IllCaseService illCaseService;

    @Override
    public int insert(Prescription prescription){
        return prescriptionDao.insert(prescription);
    }

    @Override
    public int insertSelective(Prescription prescription){
        return prescriptionDao.insertSelective(prescription);
    }

    @Override
    public int insertList(List<Prescription> prescriptions){
        return prescriptionDao.insertList(prescriptions);
    }

    @Override
    public int update(Prescription prescription){
        return prescriptionDao.update(prescription);
    }

    @Override
    public Prescription findon(Prescription prescription){
        List<Prescription> list=prescriptionDao.findAll(prescription);
        if (list==null || list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<Prescription> findAll(Prescription prescription){
        return prescriptionDao.findAll(prescription);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Map deailPres(Prescription prescription) throws Exception {
        Map<Object,Object> map = new HashMap<Object,Object>();
        //病历表更新
        IllCase illCase = new IllCase();
        illCase.setIllCaseId(prescription.getIllCaseId());
        switch (prescription.getReserve5()){
            case "0" :
                illCase.setIllCaseState(4);
                break;
            case "1" :
                illCase.setIllCaseState(3);
                break;
        }
        illCase.setVisit(prescription.getReserve3());
        illCase.setIllCaseNote(prescription.getReserve2());
        boolean isUpdate = true;

        int resut_update = illCaseService.update(illCase);
        if(resut_update == 0 ){
            isUpdate = false;
        }
        if(isUpdate){
            map.put("isUpdate","病历更新成功");
        }else{
            Exception e = new Exception("更新失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw e;
        }

        boolean isInsert = true;

        if(prescription.getReserve5().equals("1")){
            map.put("isInsert","已进入会诊");
        }else {
            int result_insert = this.insert(prescription);
            if( result_insert == 0 ){
                isInsert = false;
            }
            if ( isInsert ){
                map.put("isInsert","开单成功");
            }else {
                Exception e = new Exception("开单失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw e;
            }
        }

        return map;
    }

    @Override
    public List<Prescription> getPrescriptionByUserId(Integer userId,Integer status) {
        if (userId !=null){
            return prescriptionDao.getPrescriptionByUserId(userId,status);
        }else {
            return null;
        }
    }
}
